package com.symaster.common.geom.canvas.listener;

import com.symaster.common.geom.PolygonComponent;
import com.symaster.common.geom.Canvas;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * @author yinmiao
 * @version 2022-07-14 14:11
 */
public class CanvasMouseListener implements MouseListener {
    private final Canvas canvas;

    public CanvasMouseListener(Canvas canvas) {
        this.canvas = canvas;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (1 == e.getButton()) {
            leftClick(e);
        }
        if (2 == e.getButton()) {
            centerClick(e);
        }
        if (3 == e.getButton()) {
            rightClick(e);
        }
    }

    private void rightClick(MouseEvent e) {
        Point point = e.getPoint();
        canvas.showPopupMenu(point.x, point.y);
    }

    private void centerClick(MouseEvent e) {

    }

    private void leftClick(MouseEvent e) {
        Component componentAtPolygon = canvas.getComponentAtPolygon(e.getPoint());
        if (componentAtPolygon == null) return;

        canvas.getComponentZOrder(componentAtPolygon);
        canvas.add(componentAtPolygon, 0);
        for (Component component : canvas.getComponents()) {
            if (!(component instanceof PolygonComponent)) continue;
            PolygonComponent<?> component1 = (PolygonComponent<?>) component;
            if (component == componentAtPolygon) {
                if (component1.isSelect()) {
                    component1.unselectPolygon();
                } else {
                    component1.selectPolygon();
                }
            } else if (!canvas.isCtrlPressed()) {
                component1.unselectPolygon();
            } else {
                component.repaint();
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        canvas.closeErrorBorder();
        canvas.requestFocus();
        if (1 == e.getButton()) {
            leftPressed(e);
        }
        if (2 == e.getButton()) {
            centerPressed(e);
        }
        if (3 == e.getButton()) {
            rightPressed(e);
        }
    }

    private void centerPressed(MouseEvent e) {

    }

    private void leftPressed(MouseEvent e) {
        Point point = e.getPoint();
        Point location = e.getComponent().getLocation();
        canvas.record();
        canvas.setPressedPoint(new Point(point.x - location.x, point.y - location.y));

        Component componentAtPolygon = canvas.getComponentAtPolygon(e.getPoint());
        if (componentAtPolygon == null && !canvas.isCtrlPressed()) {
            Component[] components = canvas.getComponents();
            if (components == null || components.length == 0) return;
            for (Component component : components) {
                if (component instanceof PolygonComponent) {
                    ((PolygonComponent<?>) component).unselectPolygon();
                }
            }
        }
    }

    private void rightPressed(MouseEvent e) {
        // TODO 右键
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        canvas.mouseReleased(e);
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
